//
//  KeBiaoViewController.m
//  ZPIOSEducation
//
//  Created by Encoder on 17/2/10.
//  Copyright © 2017年 mini1. All rights reserved.
//

#import "KeBiaoViewController.h"
#import "ZPSelectCurriculumCell.h"
@interface KeBiaoViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>{
    MBProgressHUD *hud;
    NSMutableArray *dataArr;
}
@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;

@end

@implementation KeBiaoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    dataArr = [NSMutableArray array];
    hud = [[MBProgressHUD alloc]initWithView:self.view];
    [self.view addSubview:hud];
    [self getschedule];
    [_collectionView registerNib:[UINib nibWithNibName:@"ZPSelectCurriculumCell" bundle:nil] forCellWithReuseIdentifier:@"ZPSelectCurriculumCell"];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return dataArr.count;
}



- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    ZPSelectCurriculumCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ZPSelectCurriculumCell" forIndexPath:indexPath];
    NSDictionary *tmp = [dataArr objectAtIndex:indexPath.item];
    cell.date.text=[tmp objectForKey:@"date"];
    cell.numberBtn.titleLabel.text = toStr([tmp objectForKey:@"sort"]);
    cell.numberLab.text = toStr([tmp objectForKey:@"sort"]);
    if ([[tmp objectForKey:@"show"] boolValue]) {
        cell.numberLab.backgroundColor = white_color;
    }else{
        cell.numberLab.backgroundColor = zFreeColor(238, 238, 238, 1);
    }
    return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    int lenI = (SCREENWIDTH-74)/6*100;
    float len= lenI/100.0;
    return CGSizeMake(len, 48);
    
}

-(void)getschedule{
    [hud showAnimated:YES];
    [[ZPNetworking sharedZPNetworking] GET:APISearch@"teacher/getschedule.json" parameters:@{@"classId":_classId} progress:nil success:^(id  _Nonnull responseObject) {
        [hud hideAnimated:YES];
        if (CodeIs200(responseObject)) {
            [dataArr removeAllObjects];
            NSArray *tmpArr= [responseObject objectForKey:@"result"];
            
            NSInteger count = ceilf(tmpArr.count/6.0)*6;
            
            for (NSInteger idx = 0 ; idx<count; idx++) {
                [dataArr addObject:@{@"date":@"",@"sort":@""}];
            }
            [dataArr replaceObjectsInRange:NSMakeRange(0, tmpArr.count) withObjectsFromArray:tmpArr];
            _coHeight.constant = (ceilf(dataArr.count/6.0))*48;
            [_collectionView setNeedsUpdateConstraints];
            [_collectionView reloadData];
        }else{
            [ZPPromptView showMessage:toStr([responseObject objectForKey:@"codeMsg"])];
        }
    } failure:^(NSError * _Nonnull error) {
        [ZPPromptView showMessage:error.localizedDescription];
        [hud hideAnimated:YES];
    }];
}
- (IBAction)cleose:(id)sender {
    [self dismissViewControllerAnimated:NO completion:nil];
}

@end
